package com.xzmzhp.service.cosmetic.impl;

import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.dao.cosmetic.CosmeticTypeMapper;
import com.xzmzhp.pojo.cosmetic.CosmeticType;
import com.xzmzhp.service.cosmetic.CosmeticTypeService;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 宋雪
 * @Date: 2020/11/15 20:30
 * @Description: 化妆品类型表数据库逻辑实现类
 */
@Service
public class CosmeticTypeServiceImpl implements CosmeticTypeService {

    @Autowired
    private CosmeticTypeMapper cosmeticTypeMapper;

    /**
     * 添加化妆品类型
     *
     * @param cosmeticTypeName
     * @return
     */
    @Override
    public ReturnData addCosmeticType(String cosmeticTypeName) {
        Integer i = cosmeticTypeMapper.addCosmeticType(cosmeticTypeName);
        if (i != null) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "化妆品类别添加失败");
        }
    }

    /**
     * 删除化妆品类型
     *
     * @param id
     * @return
     */
    @Override
    public ReturnData deleteCosmeticType(Integer id) {
        Integer i = cosmeticTypeMapper.deleteCosmeticType(id);
        if (i != null) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "化妆品类别删除失败");
        }
    }

    /**
     * 根据id更新化妆品类型
     *
     * @param cosmeticType
     * @return
     */
    @Override
    public ReturnData updateCosmeticType(CosmeticType cosmeticType) {
        Integer i = cosmeticTypeMapper.updateCosmeticType(cosmeticType);
        if (i != null) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "您的项目出现了不可能出现的错误");
        }
    }

    /**
     * 查询所有化妆品类型
     *
     * @return ReturnData
     */
    @Override
    public ReturnData selectCosmeticTypesAll() {
        List<CosmeticType> cosmeticTypes = cosmeticTypeMapper.selectCosmeticTypesAll();
        if (cosmeticTypes != null) {
            PageInfo<CosmeticType> pageInfo = new PageInfo<>(cosmeticTypes);
            return ReturnData.ok(cosmeticTypes,pageInfo.getTotal());
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "没有找到化妆品类别信息");
        }
    }

    /**
     * 根据id查询化妆品类型
     *
     * @param id
     * @return
     */
    @Override
    public ReturnData selectCosmeticTypeById(Integer id) {
        CosmeticType cosmeticType = cosmeticTypeMapper.selectCosmeticTypeById(id);
        if (cosmeticType != null) {
            return ReturnData.ok(cosmeticType);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "没有找到该id的化妆品种类");
        }
    }

    @Override
    public ReturnData selectCosmeticTypeByName(String cosmeticTypeName) {
        List<CosmeticType> list = cosmeticTypeMapper.selectCosmeticTypeByName(cosmeticTypeName);
        if (list != null) {
            return ReturnData.ok(list);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "没有找到该名称的化妆品种类");
        }
    }
}
